package com.hk.sync.address.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hk.sync.address.bean.Node;
import com.hk.sync.address.service.USDTService;
import com.hk.sync.address.util.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class USDTService {
    private Node node;

    //获取地址
    public String getNewAddress() {
        JSONObject json = doRequest("getnewaddress");
        if (isError(json)) {
            log.error("ERROR  >> USDT Service RPC: getnewaddress  获取USDT地址失败:{}", json);
            return "";
        }
        String addr = json.getString("result");
        log.info("USDT Service RPC: getnewaddress : {} ", addr);
        return addr;
    }

    //导入私钥
    public JSONObject importPrivkey(String wif) {
        boolean rescan = false;
        JSONObject json = doRequest("importprivkey", wif, "", rescan);
        if (isError(json)) {
            log.error("ERROR  >> USDT Service RPC:   importprivkey   出错", json);
        }
        return json;
    }

    //加锁钱包
    public void walletLock() {
        JSONObject json = doRequest("walletlock");
        if (isError(json)) {
            log.error("ERROR  >>  USDT Service RPC:  walletlock  获取信息:{}", json);
        }
    }

    //解锁钱包
    public void walletPassphrase(int second) {
        JSONObject json = doRequest("walletpassphrase", node.getWalletphrase(), second);
        if (isError(json)) {
            log.error("ERROR  >>  USDT Service RPC: walletpassphrase  获取信息:{}", json);
        }
    }

    //节点地址数量
    public int addressNumberByAccount(String account) {
        JSONObject json = doRequest("getaddressnumberbyaccount", account);
        if (isError(json)) {
            log.error("ERROR  >>  USDT Service RPC: getaddressnumberbyaccount  获取信息:{}", json);
            return 0;
        }
        int number = json.getInteger("result");
        log.info("USDT Service RPC 节点地址数量 : ", number);
        return number;
    }

    //发送请求
    private JSONObject doRequest(String method, Object... params) {
        JSONObject res = null;
        try {
            JSONObject param = new JSONObject();
            param.put("id", System.currentTimeMillis() + "");
            param.put("jsonrpc", "2.0");
            param.put("method", method);
            if (params != null) {
                param.put("params", params);
            }
            String creb = Base64.encodeBase64String((node.getUsername() + ":" + node.getPassword()).getBytes());
            Map<String, String> headers = new HashMap<>(2);
            headers.put("Authorization", "Basic " + creb);
            res = JSON.parseObject(HttpUtil.jsonPost(node.getUrl(), headers, param.toJSONString()));
        } catch (Exception e) {
            log.error("USDT Service doRequest : " + method + " 调用出错  " + e.getMessage());
        }
        return res;
    }

    // error 处理
    private boolean isError(JSONObject json) {
        if (json == null || (StringUtils.isNotEmpty(json.getString("error")) && json.get("error") != "null")) {
            return true;
        }
        return false;
    }

    public Node getNode() {
        return node;
    }

    public void setNode(Node node) {
        this.node = node;
    }
}
